
const db = require("../database/models");
const Order_info = db.order_info;
const Order_express = db.order_express;
const Order_check = db.order_check;
const Op = db.Sequelize.Op;

const Trans_employee = db.trans_employee;

Order_info.belongsTo(Trans_employee,{
    foreignKey:'trans_p_code',
    sourceKey:'code',
    onDelete:'CASCADE',
    onUpdate:'CASCADE'
});
Order_info.hasOne(Order_check,{
    foreignKey:'order_id',
    sourceKey:'order_id',
    onDelete:'CASCADE',
    onUpdate:'CASCADE'
});
Order_check.belongsTo(Order_info,{
    foreignKey:'order_id',
    sourceKey:'order_id',
    onDelete:'CASCADE',
    onUpdate:'CASCADE'
});
Order_check.hasOne(Order_express,{
    foreignKey:'order_id',
    sourceKey:'order_id',
    onDelete:'CASCADE',
    onUpdate:'CASCADE'
});
Order_express.belongsTo(Order_check,{
    foreignKey:'order_id',
    sourceKey:'order_id',
    onDelete:'CASCADE',
    onUpdate:'CASCADE'
});

// Retrieve all from the database.
exports.getOrderInfoAll = (req, res) => {
    const offset = req.query.offset?parseInt(req.query.offset):0;
    const limit = req.query.limit?parseInt(req.query.limit):10;
    const all = req.query.mode==='all'?true:false;
    const hiden = req.query.mode==='hiden'?true:false;
    const mode = all||hiden?false:true;
    const condition = hiden
    ?{
      'deletedAt':{
        [Op.not]:null
      }  
    }
    :{};
    
  
    Order_info.findAndCountAll({ offset: offset, limit: limit ,paranoid:mode,where:condition,
        include:[{
          model:Trans_employee
        }]})
      .then(data => {
        res.send(data);
      })
      .catch(err => {
        res.status(500).send({
          message:
            err.message || "Some error occurred while retrieving order_info."
        });
      });
    
  };
  
  // Retrieve a single one with attr and value
  exports.getOrderInfoByAttr = (req, res) => {
    const offset = req.query.offset?parseInt(req.query.offset):0;
    const limit = req.query.limit?parseInt(req.query.limit):10;
    const all = req.query.mode==='all'?true:false;
    const hiden = req.query.mode==='hiden'?true:false;
    const mode = all||hiden?false:true;
  
    const attr = req.params.attr;
    const value = req.params.value;
    const condition = {};
    condition[attr]=value;
    if(!all){
      condition['deletedAt']=hiden
      ?{
          [Op.not]:null 
      }
      :null;
    };
    Order_info.findAndCountAll({ 
      where: condition , offset:offset,limit:limit,paranoid:mode,
      include:[{
        model:Trans_employee
      }]
    }).then(data => {
        res.send(data);
      })
      .catch(err => {
        res.status(500).send({
          message:
            err.message || "Some error occurred while retrieving order_info."
        });
      });
    
  };
  
  // Find a single one with an id
  exports.getOneOrderInfo = (req, res) => {
      const id = req.params.id;
      const mode = req.query.mode=='all'?false:true;
  
      Order_info.findByPk(id,{paranoid:mode,
        include:[{
          model:Trans_employee
        }]})
        .then(data => {
          res.send(data);
        })
        .catch(err => {
          res.status(500).send({
            message: "Error retrieving Order_info with id=" + id
          });
        });
    
  };

  // Create and Save
exports.sendApplication = (req, res) => {
    // Validate request
if (!req.body||req.body.length===0) {
  res.status(400).send({
    message: "Content can not be empty!"
  });
  return;
}

// Create a Order_express
const order_express = req.body;

// Save Order_express in the database
Order_express.create(order_express)
  .then(data => {
    res.send(data);
  })
  .catch(err => {
    res.status(500).send({
      message:
        err.message || "Some error occurred while creating the Order_express."
    });
  });

};

// Create and Save All
exports.sendApplicationAll = (req, res) => {
// Validate request
if (!req.body||req.body.length===0) {
  res.status(400).send({
    message: "Content can not be empty!"
  });
  return;
}

// Create Admin_groups
const admin_groups = req.body;

// Save Admin_groups in the database
Order_express.bulkCreate(admin_groups)
.then(data => {
  res.send(data);
})
.catch(err => {
  res.status(500).send({
    message:
      err.message || "Some error occurred while creating the Order_express."
  });
});

};